package chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : Send
 * @Author : 54061
 * @Date: 2022/2/25 18:56
 */
public class Send  implements Runnable {
    @Override
    public void run() {
        DatagramSocket sendSocket = null;
        try {
            //创建发送方的Socket对象
            sendSocket = new DatagramSocket();
            //创建封装数据的数据包对象
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String msg = null;
            while ((msg = reader.readLine()) != null) {
                byte[] b = msg.getBytes();
                InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
                DatagramPacket packet = new DatagramPacket(b, b.length, inetAddress, 12345);
                //发送数据
                sendSocket.send(packet);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            sendSocket.close();
        }
    }
}
